package edu.cug.water.crawler.analyze.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.cug.water.crawler.analyze.service.EsAnalyzeService;
import edu.cug.water.crawler.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/es")
public class EsAnalyzeController {


    @Autowired
    EsAnalyzeService esAnalyzeService;

    @RequestMapping("/twoAggsData")
    public R getGeneralAggsData(String indexName, String field1, String field2) {
        return R.ok().put("data", esAnalyzeService.getAnalyzeData(indexName, field1, field2));
    }

    @RequestMapping("/oneAggsData")
    public R getGeneralOneAggsData(String indexName, String field1) {
        return R.ok().put("data", esAnalyzeService.getAnalyzeData(indexName, field1, null));
    }

    /**
     * 参数样例：
     * {
     * 	"stationCodes":["10900300", "11000700"]
     * }
     * @param stationCodes
     * @return
     */
    @RequestMapping("/getDataByStationCodes")
    public R getDataByStationCodes(@RequestBody JSONObject stationCodes) {
        JSONArray codes = stationCodes.getJSONArray("stationCodes");
        List<String> list = codes.toJavaList(String.class);
        return R.ok().put("data", esAnalyzeService.getDataByStationCodes(list));
    }

    /**
     * 获取水质数据(根据断面名称)
     * 参数样例：
     * {
     * 	"croSectionOfRivers":["石景山区-永定河-南大荒桥", "唐山市-滦河-大黑汀水库"]
     * }
     * @param croSectionOfRivers
     * @return
     */
    @RequestMapping("/getDataByCroSectionOfRiver")
    public R getDataByCroSectionOfRiver(@RequestBody JSONObject croSectionOfRivers) {
        JSONArray codes = croSectionOfRivers.getJSONArray("croSectionOfRivers");
        List<String> list = codes.toJavaList(String.class);
        return R.ok().put("data", esAnalyzeService.getDataByCroSectionOfRivers(list));
    }

}
